package com.supermap.geometry.serverservice;

import com.supermap.geometry.service.GetDatasetGrpc;
import com.supermap.geometry.service.GetDatasetReply;
import com.supermap.geometry.service.GetDatasetRequest;
import com.supermap.protos.geometry.Dataset;
import com.supermap.protos.geometry.Feature;
import com.supermap.protos.geometry.Geometry;
import com.supermap.protos.geometry.Point;
import io.grpc.stub.StreamObserver;

import java.util.logging.Logger;

/**
 * @author liqs
 * @version 1.0
 * @date 2021/10/21 11:18
 */
public class GetDatasetImpl extends GetDatasetGrpc.GetDatasetImplBase {

    private static Logger logger = Logger.getLogger(GetDatasetImpl.class.toString());

    @Override
    public void getDataset(GetDatasetRequest req, StreamObserver<GetDatasetReply> responseObserver) {
        String datasetname = req.getDatasetname();
        logger.info("start getDataset , dataset name is " + datasetname );
        GetDatasetReply datasetReply = getDatasetReply(datasetname);
        responseObserver.onNext(datasetReply);
        responseObserver.onCompleted();
    }

    /**
     * 模拟服务端根据客户端提供的数据集名称，返回数据集信息
     * @param datasetname
     * @return
     */
    private GetDatasetReply getDatasetReply(String datasetname) {
        Dataset.ProtoDataset.Builder datasetBuilder = Dataset.ProtoDataset.newBuilder();

        // 增加属性字段信息
        Dataset.datasetField datasetFields1 = getDatasetFields("key1", "value1", "string");
        Dataset.datasetField datasetFields2 = getDatasetFields("key2", "0", "int");
        datasetBuilder.addField(datasetFields1);
        datasetBuilder.addField(datasetFields2);

        // 构造数据集信息
        Dataset.datasetInfo.Builder datasetInfoBuilder = Dataset.datasetInfo.newBuilder();
        datasetInfoBuilder.setDatasetName(datasetname);
        datasetInfoBuilder.setGeoType(Geometry.GeoType.POINT);
        datasetInfoBuilder.setPrj("4326");
        Dataset.datasetBounds.Builder datasetBoundsBuilder = Dataset.datasetBounds.newBuilder();
        datasetBoundsBuilder.setLeft(-180);
        datasetBoundsBuilder.setBottom(-90);
        datasetBoundsBuilder.setRight(180);
        datasetBoundsBuilder.setTop(90);
        datasetInfoBuilder.setDatasetBounds(datasetBoundsBuilder.build());
        datasetBuilder.setDatasetInfo(datasetInfoBuilder.build());

        // build feature
        Feature.ProtoFeature.Builder Featurebuilder = Feature.ProtoFeature.newBuilder();
        Geometry.Fields geometryFields1 = getGeometryFields("testkey1", "value1", "string");
        Geometry.Fields geometryFields2 = getGeometryFields("testkey2", "33", "int");
        Featurebuilder.addField(geometryFields1);
        Featurebuilder.addField(geometryFields2);
        Featurebuilder.setProtopoint(getProtoPoint());
        datasetBuilder.addFeature(Featurebuilder.build());
        datasetBuilder.addFeature(Featurebuilder.build());
        datasetBuilder.addFeature(Featurebuilder.build());
        Dataset.ProtoDataset dataset = datasetBuilder.build();

        // 构造返回参数
        GetDatasetReply.Builder builder = GetDatasetReply.newBuilder();
        builder.setProtoDataset(dataset);
        builder.setDatasetlength(dataset.toByteArray().length);
        return builder.build();
    }

    public Point.ProtoPoint getProtoPoint() {
        Point.ProtoPoint.Builder pointBuilder = Point.ProtoPoint.newBuilder();
        Geometry.ProtoGeometry.Builder geometry = Geometry.ProtoGeometry.newBuilder();
        geometry.setGeowkt("POINT (1 1)");
        pointBuilder.setGeometry(geometry.build());
        pointBuilder.setGeotype(Geometry.GeoType.POINT);
        Point.ProtoPoint protoPoint = pointBuilder.build();
        return protoPoint;
    }

    public Geometry.Fields getGeometryFields(String key, String value, String valueType) {
        Geometry.Fields.Builder fieldsbuild1 = Geometry.Fields.newBuilder();
        fieldsbuild1.setKey(key);
        fieldsbuild1.setValueType(valueType);
        fieldsbuild1.setValue(value);
        return fieldsbuild1.build();
    }

    public Dataset.datasetField getDatasetFields(String key, String defaultvalue, String valueType) {
        Dataset.datasetField.Builder fieldsbuild1 = Dataset.datasetField.newBuilder();
        fieldsbuild1.setKey(key);
        fieldsbuild1.setValueType(valueType);
        fieldsbuild1.setDefaultValue(defaultvalue);
        return fieldsbuild1.build();
    }


}
